package com.tsd.job.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.job.entity.SysTaskJob;
import com.tsd.job.entity.SysTaskJobExt;
import com.tsd.job.service.SysTaskJobService;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysTaskJob定时任务 控制层
 *
 * @author huawei
 * @date 2022/02/20 16:29:26
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "系统-定时任务", description = "定时任务的API描述")
public class SysTaskJobController extends BaseController {

    @Resource
    private SysTaskJobService sysTaskJobService;

    @ApiOperation(value = "根据sid获取定时任务", notes = "根据sid获取定时任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "定时任务sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getSysTaskJobBySid.action", method = RequestMethod.POST)
    public HttpResult<SysTaskJob> getSysTaskJobBySid() {
        HttpResult<SysTaskJob> hr = new HttpResult<SysTaskJob>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            SysTaskJob sysTaskJob = sysTaskJobService.queryBySid(sid);
            hr.setData(sysTaskJob);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除定时任务", notes = "根据id删除定时任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "定时任务sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteSysTaskJobs.action", method = RequestMethod.POST)
    public HttpResult<String> deleteSysTaskJobs() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            sysTaskJobService.deleteSysTaskJobs(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存定时任务", notes = "保存定时任务，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "定时任务", required = true, paramType = "body", dataType = "SysTaskJob")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSysTaskJob.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysTaskJob() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            SysTaskJobExt t = JSON.parseObject(json, SysTaskJobExt.class);
            sysTaskJobService.saveSysTaskJob(t, opUser);
            sysTaskJobService.updateQuartzFactory4Spring(t);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "获取目标Bean及目标方法列表", notes = "获取目标Bean及目标方法列表")
    @ResponseBody
    @RequestMapping(value = "/getJobObjectAndMethodList.action", method = RequestMethod.POST)
    public HttpResult<Object> getJobObjectAndMethodList() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            List<Object> list = sysTaskJobService.getJobObjectAndMethodList();
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询定时任务", notes = "分页查询定时任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findSysTaskJobs.action", method = RequestMethod.POST)
    public HttpResult<List<SysTaskJobExt>> findSysTaskJobs() {
        HttpResult<List<SysTaskJobExt>> hr = new HttpResult<List<SysTaskJobExt>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "mode", "mode", false, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            List<SysTaskJobExt> list = sysTaskJobService.findSysTaskJobs(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "启动定时任务", notes = "启动定时任务")
    @ResponseBody
    @RequestMapping(value = "/startQuartz.action", method = RequestMethod.POST)
    public HttpResult<Object> startQuartz() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            List<String> list = super.getParamsList("sid_list", String.class, true);
            sysTaskJobService.startQuartz(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "停止定时任务", notes = "停止定时任务")
    @ResponseBody
    @RequestMapping(value = "/stopQuartz.action", method = RequestMethod.POST)
    public HttpResult<Object> stopQuartz() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            List<String> list = super.getParamsList("sid_list", String.class, true);
            sysTaskJobService.stopQuartz(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
